﻿//////////////////////////////////////////////////////////////////////////////////
//
// ActionScript 3.0 Common Library (MIT License)
// Copyright (c) 2009 AS3CommonCode | Tony Birleffi | http://www.mosaiceye.com
// 
// Permission is hereby granted, free of charge, to any person
// obtaining a copy of this software and associated documentation
// files (the "Software"), to deal in the Software without
// restriction, including without limitation the rights to use,
// copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following
// conditions:
// 
// The above copyright notice and this permission notice shall be
// included in all copies or substantial portions of the Software.
// 
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
// OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
// HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
// WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
// OTHER DEALINGS IN THE SOFTWARE.
//
//////////////////////////////////////////////////////////////////////////////////

package org.as3commoncode.utils
{
	import flash.text.TextField;
	import flash.text.TextFormat;
	import flash.text.TextFieldType;
	import flash.text.TextFieldAutoSize;
	import flash.text.AntiAliasType;
	import flash.text.StyleSheet;	

	/**
	 * This is a utility class for writing textfields.
     * 
     * @author Tony Birleffi
     */
	public class TextFieldUtil
	{
		/**
		 * Creates a textfield with the font embeded.
		 * 
		 * @example
		 * <listing>
		 * import org.as3commoncode.utils.TextFieldUtil;
		 * var tf:TextField = TextFieldUtil.createFormatField("Welcome To Proximity!!", format, 50, 50);
		 * </listing>
		 * 
		 * @param value			string value.
		 * @param format		default is null, and won't use any embeded fonts.
		 * @param x				default is 0, x position.
		 * @param y				default is 0, y position.
		 * @param width			default is 150, width.
		 * @param height		default is 18, height.
		 * @param align			default is 'left', text alignment.
		 * @param select		default is false, whether or not the text is selectable.
		 * @param multi			default is false, whether the textfield has multiple lines or not.
		 * @param wordWrap		default is false, whether the textfield word wraps or not.
		 * @param thickness		default is 0, adds thickness to the textfield.
		 * @param sharpness		default is 0, adds sharpness to the textfield.
		 * @param init			default is null, however you can pass any additional parameters, { alpha:50 }.
		 * @return TextField.
		 */	  
		public static function createFormatField(value:String = '', format:TextFormat = null, x:Number = 0, y:Number = 0, 
												  width:Number = 150, height:Number = 18, align:String = null, 
												  select:Boolean = false, multi:Boolean = false, wordWrap:Boolean = false, 
												  thickness:Number = 0, sharpness:Number = 0, init:Object = null):TextField
		{
			// Define.
			var tf:TextField = new TextField();
            
            // Setup properties.
            tf.text = value;
            tf.type = TextFieldType.DYNAMIC;
			tf.x = x;
            tf.y = y;
            tf.width = width;
            tf.height = height;
			tf.embedFonts = (format == null) ? false : true;
			if(format != null) tf.autoSize = (align == null) ? TextFieldAutoSize.LEFT : align;
			tf.selectable = select;
			tf.multiline = multi;
			tf.wordWrap = wordWrap;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.thickness = thickness;
			tf.sharpness = sharpness;
			tf.setTextFormat((format != null) ? format : null);
			tf.defaultTextFormat = (format != null) ? format : null;
			
			// Pass any additional properties if need be here.
			if (init != null) for(var it:String in init) tf[ it ] = init[ it ];
			
			// Return the formatted textfield.
            return tf;
		}
		
		/**
		 * Creates an html textfield with formatting from a style sheet.
		 * 
		 * @example
		 * <listing>
		 * import org.as3commoncode.utils.TextFieldUtil;
		 * var htmlTf:TextField = TextFieldUtil.createHtmlField("<span>Welcome To Proximity!!</span>", styleSheet, 50, 50);
		 * </listing>
		 * 
		 * @param value			string value.
		 * @param format		use a StyleSheet object, default is null, and won't use any embeded fonts.
		 * @param x				default is 0, x position.
		 * @param y				default is 0, y position.
		 * @param width			default is 300, width.
		 * @param height		default is 300, height.
		 * @param align			default is 'left', text alignment.
		 * @param select		default is true, whether or not the text is selectable.
		 * @param multi			default is true, whether the textfield has multiple lines or not.
		 * @param wordWrap		default is true, whether the textfield word wraps or not.
		 * @param init			default is null, however you can pass any additional parameters, { alpha:50 }.
		 * @return TextField.
		 */	
		public static function createHtmlField(value:String = '', format:StyleSheet = null, x:Number = 0, y:Number = 0, 
													width:Number = 300, height:Number = 300, align:String = null, select:Boolean = true, 
													multi:Boolean = true, wordWrap:Boolean = true, init:Object = null):TextField
		{
			// Define.
			var tf:TextField = new TextField();
            
            // Setup properties.
            tf.x = x;
            tf.y = y;
            tf.width = width;
            tf.height = height;
			tf.embedFonts = (format == null) ? false : true;
			if(format != null) tf.autoSize = (align == null) ? TextFieldAutoSize.LEFT : align;
			tf.selectable = select;
			tf.multiline = multi;
			tf.wordWrap = wordWrap;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			if(format != null) tf.styleSheet = format;
			tf.type = TextFieldType.DYNAMIC;
			tf.htmlText = value;
			
			// Pass any additional properties if need be here.
			if (init != null) for(var it:String in init) tf[ it ] = init[ it ];
			
			// Return the formatted textfield.
            return tf;
		}
		
		/**
		 * Creates an input textfield with the font embeded.
		 * 
		 * @example
		 * <listing>
		 * import org.as3commoncode.utils.TextFieldUtil;
		 * var inputTf:TextField = TextFieldUtil.createInputField("Input text here", format, 50, 50);
		 * </listing>
		 * 
		 * @param value			string value.
		 * @param format		default is null, and won't use any embeded fonts.
		 * @param x				default is 0, x position.
		 * @param y				default is 0, y position.
		 * @param width			default is 150, width.
		 * @param height		default is 18, height.
		 * @param align			default is 'left', text alignment.
		 * @param select		default is true, whether or not the text is selectable.
		 * @param multi			default is false, whether the textfield has multiple lines or not.
		 * @param wordWrap		default is false, whether the textfield word wraps or not.
		 * @param password		default is false, whether the textfield is for a password or not.
		 * @param maxChar		default is 0, max number of characters for input.
		 * @param restrict		default is '', restricted characters, 'A-Z a-z'. 
		 * @param doubleClick	default is false, if the textfield is double clickable or not.
		 * @param init			default is null, however you can pass any additional parameters, { alpha:50 }.
		 * @return TextField.
		 */	  
		public static function createInputField(value:String = '', format:TextFormat = null, x:Number = 0, y:Number = 0, 
													width:Number = 150, height:Number = 18, select:Boolean = true, 
													multi:Boolean = false, wordWrap:Boolean = false, password:Boolean = false,
													maxChar:Number = 0, restrict:String = null, doubleClick:Boolean = false, init:Object = null):TextField
		{
			// Define.
			var tf:TextField = new TextField();
            
            // Setup properties.
            tf.text = value;
            tf.type = TextFieldType.INPUT;
            tf.displayAsPassword = password;
            tf.doubleClickEnabled = doubleClick;
            tf.maxChars = maxChar;
            tf.restrict = restrict;
			tf.x = x;
            tf.y = y;
            tf.width = width;
            tf.height = height;
			tf.embedFonts = (format == null) ? false : true;
			tf.selectable = select;
			tf.multiline = multi;
			tf.wordWrap = wordWrap;
			tf.antiAliasType = AntiAliasType.ADVANCED;
			tf.setTextFormat((format != null) ? format : null);
			tf.defaultTextFormat = (format != null) ? format : null;
			
			// Pass any additional properties if need be here.
			if (init != null) for(var it:String in init) tf[ it ] = init[ it ];
			
			// Return the formatted textfield.
            return tf;
		}
	}
}
